<?php
declare(strict_types=1);

namespace SixShop\Message\Entity;

use think\Model;

/**
 * 用户通知实体类
 */
class NotificationEntity extends Model
{
    // 设置表名
    protected $name = 'message_notifications';
    
    // 设置字段信息
    protected $schema = [
        'id'          => 'int',
        'user_id'     => 'int',
        'title'       => 'string',
        'content'     => 'string',
        'type'        => 'int',
        'is_read'     => 'int',
/*        'read_time'   => 'int',
        'create_time' => 'int',
        'update_time' => 'int',
        'delete_time' => 'int',*/
    ];
    
    // 自动时间戳
    protected $autoWriteTimestamp = true;
    
    // 软删除
    protected $deleteTime = 'delete_time';
    
    /**
     * 获取通知类型文本
     */
    public function getTypeTextAttr()
    {
        $types = [
            1 => '系统通知',
            2 => '活动通知',
            3 => '订单通知',
            4 => '物流通知',
            5 => '其他通知',
        ];
        
        return $types[$this->type] ?? '未知类型';
    }
    
    /**
     * 获取是否已读文本
     */
    public function getIsReadTextAttr()
    {
        $statuses = [
            0 => '未读',
            1 => '已读',
        ];
        
        return $statuses[$this->is_read] ?? '未知状态';
    }
    
    /**
     * 标记为已读
     */
    public function markAsRead()
    {
        if ($this->is_read == 0) {
            $this->is_read = 1;
            $this->read_time = time();
            $this->save();
        }
        
        return $this;
    }
    
    /**
     * 关联用户
     */
    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id');
    }
}
